﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.Organizations
{
    public partial class OrganizationsTree : SERP.Modules.ModuleBase
    {
        private int parentId = -1;
        private int rootType = 0;
        private bool autoPostBack = true;
        private bool isLoad = false;
        public bool AutoPostBack
        {
            get
            {
                return autoPostBack;
            }
            set
            {
                autoPostBack = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                LoadData();
            }
        }
        public OrganizationsEntity GetSelectedOrganization()
        {
            if (tv.SelectedNode != null)
            {
                return OrganizationsController.GetOrganizationById(Controller.ConvertInt(tv.SelectedNode.Value));
            }
            return null;
        }
        public List<int> GetSelectedAndChildId()
        {
            List<int> lsId = new List<int>();
            if (tv.SelectedNode != null)
            {
                AddRescusive(tv.SelectedNode, lsId);
            }
            else
            {
                foreach (TreeNode node in tv.Nodes)
                {
                    AddRescusive(node, lsId);
                }
            }
            return lsId;
        }
        private void AddRescusive(TreeNode node, List<int> lsId)
        {
            int id = Controller.ConvertInt(node.Value);
            lsId.Add(id);
            foreach (TreeNode sNode in node.ChildNodes)
            {
                AddRescusive(sNode, lsId);
            }
        }
        public int ParentId
        {
            get
            {
                return parentId;
            }
            set
            {
                parentId = value;
            }
        }
        public int RootTyoe
        {
            get
            {
                return rootType;
            }
            set
            {
                rootType = value;
            }
        }
        public void LoadData()
        {
            if (isLoad) return;
            isLoad = true;
            foreach (OrganizationsEntity o in OrganizationsController.GetOrganizationByParent(parentId))
            {
                if (rootType == 0 || o.OrgType == rootType)
                {
                    TreeNode node = new TreeNode(o.OrgCode + ":" + o.OrgName, o.OrganizationId.ToString());
                    tv.Nodes.Add(node);
                    if (!autoPostBack)
                    {
                        node.SelectAction = TreeNodeSelectAction.Expand;
                    }
                    if (Session[this.ClientID] != null && Session[this.ClientID].ToString() == o.OrganizationId.ToString())
                    {
                        node.Selected = true;
                    }
                    LoadDataRescusive(node, o.OrganizationId);
                    node.ExpandAll();
                }
            }
        }
        private void LoadDataRescusive(TreeNode parentNode, int parentId)
        {
            foreach (OrganizationsEntity o in OrganizationsController.GetOrganizationByParent(parentId))
            {
                TreeNode node = new TreeNode(o.OrgCode + ":" + o.OrgName, o.OrganizationId.ToString());
                if (!autoPostBack)
                {
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                if (Session[this.ClientID] != null && Session[this.ClientID].ToString() == o.OrganizationId.ToString())
                {
                    node.Selected = true;
                }
                parentNode.ChildNodes.Add(node);
                LoadDataRescusive(node, o.OrganizationId);
            }
        }
        public event EventHandler SelectedNodeChanged;
        protected void tv_SelectedNodeChanged(object sender, EventArgs e)
        {
            Session[this.ClientID] = tv.SelectedNode.Value;
            if (SelectedNodeChanged != null)
                SelectedNodeChanged(sender, e);
        }
    }
}
